#include "pageadminupdateinfo.h"
#include "ui_pageadminupdateinfo.h"
#include "widget.h"
#include <QDebug>

pageAdminUpdateInfo::pageAdminUpdateInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::pageAdminUpdateInfo)
{
    ui->setupUi(this);
    m_logManager = new LogManager(this);
    connect(ui->btnSave,&QPushButton::clicked,this,&pageAdminUpdateInfo::onSaveChanges);
}

pageAdminUpdateInfo::~pageAdminUpdateInfo()
{
    delete ui;

}

void pageAdminUpdateInfo::receiveUsername(const QString &username)
{
    currentUsername = username;
    qDebug() << "收到用户名:" << currentUsername;
    // 根据 currentUsername 的值设置 lineEditUserName 的可用性
    if (currentUsername == "admin") {
        ui->lineEditUserName->setEnabled(false);
        ui->lineEditUserName->setText("admin");
    } else {
        ui->lineEditUserName->setEnabled(true);
        ui->lineEditUserName->setText("");
    }
}

void pageAdminUpdateInfo::onSaveChanges()
{
    QString newUsername = ui->lineEditUserName->text();
    QString newPassword = ui->lineEditPassword->text();
    QString newPhone = ui->lineEditPhone->text();
    QString newEmail = ui->lineEditMail->text();
    if(newUsername.isEmpty() ){
        emit sendMessage("用户名为空，请输入有效内容!");
    }else if(newPassword.isEmpty() ){
        emit sendMessage("密码为空，请输入有效内容!");
    }else if(newEmail.isEmpty() ){
        emit sendMessage("邮箱为空，请输入有效内容!");
    }else if(newPhone.isEmpty() ){
        emit sendMessage("手机号码为空，请输入有效内容!");
    }else{
        userInfo = m_userMgr.queryUserByUsernamePhoneMail(currentUsername,"","");
        if(currentUsername == "admin" && newUsername != "admin"){
            emit sendMessage("管理员用户名无法修改!");
            // 记录操作日志
            if (m_logManager) {
                m_logManager->recordSystemLog(
                            QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                            "OPERATION",                                     // 日志类型
                            "INFO",                                          // 日志级别
                            QString("管理员[%1]修改个人信息失败!").arg(currentUsername),   //操作内容
                            userInfo["user_id"].toInt(),               // 用户ID
                             -1                                               // 设备ID
                        );
            }
        }else{
            if (m_userMgr.updateByUsername(currentUsername,newUsername, newPassword,newEmail,newPhone, "")) {
                emit display(Widget::INDEX_MAINBAR);
                // 记录操作日志
                if (m_logManager) {
                    m_logManager->recordSystemLog(
                                QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                "OPERATION",                                     // 日志类型
                                "INFO",                                          // 日志级别
                                QString("管理员[%1]修改个人信息成功!").arg(currentUsername),   //操作内容
                                userInfo["user_id"].toInt(),               // 用户ID
                            -1                                               // 设备ID
                            );
                }
                // 清空输入框
                ui->lineEditUserName->clear();
                ui->lineEditPassword->clear();
                ui->lineEditPhone->clear();
                ui->lineEditMail->clear();
            } else {
                emit sendMessage("错误:"+m_userMgr.getLastError());
                // 记录操作日志
                if (m_logManager) {
                    m_logManager->recordSystemLog(
                                QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"),
                                "OPERATION",                                     // 日志类型
                                "INFO",                                          // 日志级别
                                QString("管理员[%1]修改个人信息失败!").arg(currentUsername),   //操作内容
                                userInfo["user_id"].toInt(),               // 用户ID
                                 -1                                               // 设备ID
                            );
                }
            }
        }
    }
}
